# One of the two sites forming a cross border
class Site < ActiveRecord::Base
  
  belongs_to :country
  
  has_many :sites_cross_borders
  has_many :cross_borders, :through => :sites_cross_borders
    
  has_one :coordinator_role, :class_name => 'Role', :as => :coordinated
  
  validates_presence_of :city
 
  def code
    self.cross_border.code unless self.cross_border.nil?
  end
  
  def matchings
    # currently cross_borders only contains two sites.
    # it is enough to keep the only one that has not the self id
    self.cross_borders.map { |xb| xb.sites.first :conditions => [ "sites.id != ?", self.id ] }
  end
  
  def matching_names
    matchings.map(&:name).join(', ')
  end
 
  def description
    return _(self.name) + _(" relationship with ") + _(matching_names)
  end
 
end
